package com.sdry.service.impl.llm;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.sdry.mapper.hpf.PositionMapper;
import com.sdry.mapper.llm.ClassesMapper;
import com.sdry.mapper.llm.EmployeeRecordMapper;
import com.sdry.mapper.llm.TeamMapper;
import com.sdry.mapper.llm.TeamRecordMapper;
import com.sdry.mapper.llm.UserMapper;
import com.sdry.model.hpf.Position;
import com.sdry.model.llm.Classes;
import com.sdry.model.llm.EmployeeRecord;
import com.sdry.model.llm.Team;
import com.sdry.model.llm.TeamRecord;
import com.sdry.model.llm.User;
import com.sdry.service.llm.EmployeeRecordService;

/**
 * 员工工作记录的service实现类
 * <p>Title: EmployeeRecordServiceImp</p>
 * @author llm
 * @date 2018年11月21日
 * @version 1.0
 */

@Service
public class EmployeeRecordServiceImp implements EmployeeRecordService {

	/**
	 * 注入员工工作记录的mapper
	 */
	@Resource
	private EmployeeRecordMapper employeeRecoredMapper;
	/**
	 * 注入班组记录的mapper
	 */
	@Resource
	private TeamRecordMapper teamRecoredMapper;
	/**
	 * 注入班组的mapper
	 */
	@Resource
	private TeamMapper teamMapper;
	/**
	 * 注入工位的mapper
	 */
	@Resource
	private PositionMapper positionMapper;
	/**
	 * 注入用户的mapper
	 */
	@Resource
	private UserMapper userMapper;
	/**
	 * 注入班次的mapper
	 */
	@Resource
	private ClassesMapper classesMapper;
	
	/**
	 * 获取员工工作记录总数
	 * @param tid  班组记录ID
	 * @return
	 * @see com.sdry.service.llm.EmployeeRecordService#countEmployeeRecord(com.sdry.model.llm.EmployeeRecord)
	 */
	@Override
	public int countEmployeeRecordByTid(EmployeeRecord employeeRecord) {
		int isSuccess = employeeRecoredMapper.countEmployeeRecordByTid(employeeRecord);
		return isSuccess;
	}

	/**
	 * 分页查询员工工作记录
	 * @param map 条件集合（页数，每页条数，查询条件）
	 * @return
	 * @see com.sdry.service.llm.EmployeeRecordService#listPageEmployeeRecord(java.util.Map)
	 */
	@Override
	public List<EmployeeRecord> listPageEmployeeRecord(Map<String, Object> map) {
		List<EmployeeRecord> list = employeeRecoredMapper.listPageEmployeeRecord(map);
		for(EmployeeRecord er : list){
			if(null != er.getPid()){
				String pname = positionMapper.getPositionByPid(er.getPid()).getPname();
				er.setPositionName(pname);
			}
		}
		return list;
	}

	/**
	 * 保存员工记录
	 * @param positionName 工位名称
	 * @param account 工号
	 * @param teamId 班组ID
	 * @return
	 * @see com.sdry.service.llm.EmployeeRecordService#saveEmployeeRecord(java.lang.String, java.lang.String)
	 */
	@Override
	public int saveEmployeeRecord(String positionName, String account, Long teamId, HttpSession session) {
		int isSuccess = 0;
		//查看班组记录，获取班组记录ID
		Date date = new Date();
		SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowTime = sdf3.format(date);
		Integer hourse = Integer.parseInt(nowTime.substring(11,13));
		Team team = (Team)session.getAttribute("team");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = "";
		//根据班次查询工作时间
		List<Classes> classesByName = classesMapper.getClassesByName(team.getCrew());
		Classes cla = classesByName.get(0);
		List<Classes> classesByWrite = classesMapper.getClassesByName("白班");
		Classes clas = classesByWrite.get(0);
		Integer writeStart = Integer.parseInt(clas.getStartTime().substring(0, 2));
		String startTime = cla.getStartTime();
		String endTime = cla.getEndTime();
		Integer start = Integer.parseInt(startTime.substring(0, 2));
		Integer end = Integer.parseInt(endTime.substring(0, 2));
		//如果是晚班的24点之后登录，验证上一天的排产单
		if(start > end && hourse <= end){
			Calendar ca = Calendar.getInstance();//得到一个Calendar的实例 
			ca.setTime(date); //设置时间为当前时间
			ca.add(Calendar.DATE, -1);
			Date last = ca.getTime();
			time = sdf.format(last);
		}else if(start < end && start < writeStart){
			Calendar ca = Calendar.getInstance();//得到一个Calendar的实例 
			ca.setTime(date); //设置时间为当前时间
			ca.add(Calendar.DATE, -1);
			Date last = ca.getTime();
			time = sdf.format(last);
		}else{
			time = sdf.format(date);
		}
		TeamRecord tr = new TeamRecord();
		tr.setTtime(time);
		tr.setTeamId(teamId);
		TeamRecord teamRecord = teamRecoredMapper.getTeamRecordByTeamRecord(tr);
		//如果有排班记录
		if(null != teamRecord){
			//把员工信息存入员工记录
			EmployeeRecord employeeRecord = new EmployeeRecord();
			//工位ID
			Position position = positionMapper.getPositionByPname(positionName);
			employeeRecord.setPid(position.getPid());
			//员工ID
			User user = userMapper.getUserByAccount(account);
			employeeRecord.setUid(user.getUid());
			//班组记录ID
			employeeRecord.setTid(teamRecord.getTid());
			//登录时间
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String loginTime = sdf2.format(date);
			employeeRecord.setLoginTime(loginTime);
			isSuccess = employeeRecoredMapper.saveEmployeeRecord(employeeRecord);
			//把员工保存的ID存入session
			session.setAttribute("employeeRecordId", isSuccess);
		}
		return isSuccess;
	}

	/**
	 * 把退出系统时间存入员工记录
	 * @param employeeRecord
	 * @return
	 * @see com.sdry.service.llm.EmployeeRecordService#updatelogoutTimeByEmployeeRecord(java.lang.String)
	 */
	@Override
	public int updatelogoutTimeByEmployeeRecord(EmployeeRecord employeeRecord) {
		return employeeRecoredMapper.updatelogoutTimeByEmployeeRecord(employeeRecord);
	}

	/**
	 * 删除员工记录
	 * @param id
	 * @return
	 * @see com.sdry.service.llm.EmployeeRecordService#deleteEmployeeRecord(java.lang.Long)
	 */
	@Override
	public int deleteEmployeeRecord(Long id) {
		return employeeRecoredMapper.deleteEmployeeRecord(id);
	}

}
